package me.moscrop.RootCommand;

import java.io.File;
import java.util.logging.Logger;

import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;

import me.moscrop.RootCommand.commands.*;
import me.moscrop.RootCommand.listeners.*;

public class RootCommand extends JavaPlugin {
	
	Logger log = Logger.getLogger("Minecraft");	
	public static String pluginDir = "plugins" + File.separator + "rootCommand";
	
	/*
	 * Custom Listeners
	 */
	private final RCPlayerListener playerListener = new RCPlayerListener();
	private final RCEntityListener entityListener = new RCEntityListener();
	private final RCBlockListener blockListener = new RCBlockListener();
	public static Kit KitCommand = new Kit(pluginDir);
	public static Warp WarpCommand = new Warp(pluginDir);
	public static Homes HomeCommand = new Homes(pluginDir);
	
	/*
	 * Need to be updated for every new command :(
	 * But a better solution than a giant IF command
	 */
	private enum Commands
	{
		GIVE, WARP, HOME, KIT, WORLD, SPAWNMOB, TOGGLE,
		TREE, MUSHROOM, TROLL, KILL, WEATHER, TIME,
		BACK, GOD, HEAL, FEED, TP, LIGHTNING, JOIN,
		KILLMOB, NULLVALUE;

		private static Commands available(String str)
	    {
	        try {
	            return valueOf(str.toUpperCase());
	        } 
	        catch (Exception ex) {
	            return NULLVALUE;
	        }
	    }   
	}
	
	@Override
	public void onEnable(){
		log.info("Enabled " + getDescription().getName() + " " + getDescription().getVersion() + ".");
						
		KitCommand.loadConfig();
		HomeCommand.loadConfig();
		WarpCommand.loadConfig();
		
		PluginManager pm = this.getServer().getPluginManager();		
		pm.registerEvent( Event.Type.PLAYER_CHAT,		playerListener, Event.Priority.Highest,	 this);
		pm.registerEvent( Event.Type.PLAYER_BED_ENTER,	playerListener, Event.Priority.Low,		 this);
		pm.registerEvent( Event.Type.PLAYER_BED_LEAVE,	playerListener, Event.Priority.Low,		 this);
		pm.registerEvent( Event.Type.PLAYER_TELEPORT,	playerListener, Event.Priority.Low,		 this);	
		pm.registerEvent( Event.Type.PLAYER_RESPAWN,	playerListener, Event.Priority.High,	 this);	
		pm.registerEvent( Event.Type.ENTITY_DEATH,		entityListener, Event.Priority.High,	 this);
		pm.registerEvent( Event.Type.ENTITY_DAMAGE,		entityListener, Event.Priority.Normal,	 this);	
		pm.registerEvent( Event.Type.BLOCK_SPREAD, 		blockListener, 	Event.Priority.Normal,	 this);
	}
 
	@Override
	public void onDisable(){
		log.info("Disabled " + getDescription().getName() + " " + getDescription().getVersion() + ".");
	}
	
	@Override
	public boolean onCommand (CommandSender sender, Command command, String label, String[] args){
		String rawCommand = command.getName();
		
		if (!(sender instanceof Player)) {
			log.info("Cannot run the command '" + rawCommand + "' in console mode!");
			return true;
		}
			
		switch (Commands.available(rawCommand)) {	
			case GIVE:
				return false; //PlaceHolder
			case HEAL:
				return Heal.healSender(sender,args);
			case FEED:
				return Feed.feedSender(sender,args);				
			case WARP:				
				return WarpCommand.onCommand(sender,args); //PlaceHolder	
			case TIME:				
				return Time.time(sender, command, label, args); //PlaceHolder
			
			case KIT:			
				return KitCommand.onCommand(sender, args); 
						
			case SPAWNMOB:				
				return Spawnmob.mob(sender, command, label, args);
			
			case KILLMOB:
				return KillMob.killmob(sender, command, label, args);
			
			case TOGGLE:
				return Toggle.toggleSender(sender,args);
			
			case TREE:				
				return Tree.generateTree(sender, command, label, args);
			
			case MUSHROOM:				
				return false;
			
			case TROLL:				
				return false;
			
			case KILL:				
				return Kill.kill(sender,args);
			
			case WEATHER:
				return Weather.weather(sender, command, label, args);
				
			case LIGHTNING:
				return Weather.lightning(sender, command, label, args);
				
			case BACK:
				return Back.back(sender);
				
			case TP:
				return TP.teleport(sender,args);
				
			case GOD:
				return God.toggle(sender, args);
			
			case HOME:
				return HomeCommand.onCommand(sender, args);
				
			case WORLD:
				return MultiWorld.world(sender, command, label, args);
				
			case JOIN:
				return MultiWorld.join(sender, command, label, args);
				
			default:
		        return false;
		}
	}
}
